<template>
  <div class="section-page">
    <h3 class="section-title">产品验收</h3>
    <div class="section-header">
      <el-button-group class="button-group mt10">
        <el-button
          type="default"
          :class="billType === 'NORMAL' ? 'btn-c-primary' : 'btn-c-default'"
          @click="billTypeChange('NORMAL')"
          >单机验收</el-button
        >
        <el-button
          type="default"
          :class="billType === 'SYSTEM' ? 'btn-c-primary' : 'btn-c-default'"
          @click="billTypeChange('SYSTEM')"
          >系统验收</el-button
        >
      </el-button-group>
      <el-form class="modify-form flex-center" inline>
        <!-- <el-form-item class="search-form-item" v-if="billType == 'SYSTEM'">
          <el-button
            class="btn-add-primary"
            icon="el-icon-plus"
            @click="addHandle"
          ></el-button>
        </el-form-item> -->
        <el-form-item class="search-form-item" v-if="billType === 'NORMAL'">
          <import-data
            :api-url="importUrl"
            title="导入数据"
            :params="importParams"
            @upload-success="uploadSuccess"
          />
        </el-form-item>
        <el-form-item class="search-form-item" v-if="billType === 'SYSTEM'">
          <import-rar-data
            :api-url="importSysUrl"
            title="导入数据"
            :params="importParams"
            @upload-success="uploadSysSuccess"
          />
        </el-form-item>
        <el-form-item class="search-form-item" v-if="billType === 'SYSTEM'">
          <a
            class="btn-40 btn-img hover"
            title="系统验收导出"
            :href="url + exportSysUrl + '?modelId=' + id"
          >
            <span class="icon-img download"></span>
          </a>
        </el-form-item>
      </el-form>
    </div>

    <div class="mt10 tab-wrap">
      <acceptance-table
        class="mt10"
        :bill-type="billType"
        :list="tableData"
        @deleteRow="deleteRow"
        @saveRow="saveRow"
      ></acceptance-table>
      <div style="padding: 10px; text-align: center">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :page-size="pageOption.pageSize"
          :total="pageOption.total"
          :page-sizes="pageOption.sizes"
          @size-change="sizeCurrentChange"
          @current-change="pageCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
    <add-dialog
      v-if="showAdd"
      :model-id="this.id"
      :dataObj="addReqObj"
      @close="addCloseHandle"
      @saveSuccess="saveSuccess"
    />
  </div>
</template>

<script>
import acceptanceApi from "@/apis/overview/acceptance.js";
import AcceptanceTable from "./AcceptanceTable";
import AddDialog from "./AddDialog.vue";
import ImportData from "@/components/ImportData.vue";
import ImportRarData from "@/components/ImportRarData.vue";

export default {
  components: { AcceptanceTable, AddDialog, ImportData, ImportRarData },
  props: ["id"],
  data() {
    return {
      url: process.env.VUE_APP_BASE_API,
      showAdd: false,
      importUrl: acceptanceApi.rarImportUrl,
      exportSysUrl: acceptanceApi.exportSysUrl,
      importSysUrl: acceptanceApi.rarSysImportUrl,
      pageOption: {
        page: 1,
        pageSize: 10,
        total: 1,
        sizes: [5, 10, 20, 30, 40, 50, 100],
      },
      billType: "NORMAL", // NORMAL: 设备库, SYSTEM: 系统库
      tableData: [],
      normalData: [],
      systemData: [],
      addReqObj: {
        modelId: this.id,
        sysCode: "",
        sysName: "",
        designOrg: "",
        productionOrg: "",
        matchOrg: "",
        qualified: "",
        sysNo: "",
      },
    };
  },
  computed: {
    importParams() {
      return { modelId: this.id };
    },
  },
  methods: {
    addHandle() {
      this.addReqObj.qualified = "";
      this.showAdd = true;
    },
    addCloseHandle(refresh) {
      if (refresh) {
      }
      this.showAdd = false;
    },
    uploadSuccess() {
      this.pageCurrentChange(1);
    },
    uploadSysSuccess() {
      this.pageCurrentChange(1);
    },
    //系统验收删除
    deleteRow(row) {
      acceptanceApi
        .deleteSystemList({
          id: row.id,
        })
        .then(({ data }) => {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
          this.pageCurrentChange(1);
        });
    },
    //系统验收保存
    saveRow(row, type) {
      if (type == "DJ") {
        if (row.qualified.code) {
          row.qualified = row.qualified.code;
        } else {
          delete row["qualified"];
        }

        row.deviceId = row.id;
        acceptanceApi.saveSystemListDJ(row).then(({ data }) => {
          this.$notify({
            title: "成功",
            message: "保存成功",
            type: "success",
          });
          this.pageCurrentChange(1);
        });
      } else {
        //   debugger
        if (row.qualified.code) {
          row.qualified = row.qualified.code;
        } else {
          delete row["qualified"];
        }

        acceptanceApi.saveSystemList(row).then(({ data }) => {
          this.$notify({
            title: "成功",
            message: "保存成功",
            type: "success",
          });
          this.pageCurrentChange(1);
        });
      }
    },

    //系统验收新增保存
    saveSuccess() {
      this.pageCurrentChange(1);
    },
    // 切换表格
    billTypeChange(type) {
      this.billType = type;
      this.pageOption.page = 1;
      this.tableData = [];
      this.requestListData();
    },
    // 分页操作
    sizeCurrentChange(size) {
      this.pageOption.pageSize = size;
      this.requestListData();
    },
    pageCurrentChange(page) {
      this.pageOption.page = page;
      this.requestListData();
    },
    // 请求表格数据
    requestListData() {
      const { page, pageSize } = this.pageOption;
      // this.tableData = this.billType == 'NORMAL' ? this.normalData : this.systemData
      if (this.billType === "NORMAL") {
        acceptanceApi
          .acceptanceList({
            modelId: this.id,
            pageNum: page,
            pageSize,
          })
          .then(({ data }) => {
            // debugger
            const { list, total } = data;
            this.tableData = list.map((item) => {
              if (
                !item.qualified ||
                Object.prototype.toString.call(item.qualified) !==
                  "[object Object]"
              ) {
                if (item.qualified === null) {
                  item.qualified = {
                    code: "Y",
                    message: "合格",
                  };
                } else if (item.qualified === "N") {
                  item.qualified = {
                    code: "N",
                    message: "不合格",
                  };
                } else if (item.qualified === "Y") {
                  item.qualified = {
                    code: "N",
                    message: "合格",
                  };
                } else {
                  item.qualified = {
                    code: "",
                    message: "",
                  };
                }
              } else {
                item.qualified = {
                  code: item.qualified.code,
                  message: item.qualified.message == "是" ? "合格" : "不合格",
                };
              }
              return {
                ...item,
                edit: false,
              };
            });
            console.log(data);

            this.pageOption.total = total;
          });
      } else {
        acceptanceApi
          .acceptanceSysList({
            modelId: this.id,
            pageNum: page,
            pageSize,
          })
          .then(({ data }) => {
            const { list, total } = data;
            // debugger
            this.tableData = list.map((item) => {
              if (item.qualified === null) {
                item.qualified = {
                  code: "Y",
                  message: "合格",
                };
              } else if (item.qualified === "N") {
                item.qualified = {
                  code: "N",
                  message: "不合格",
                };
              } else if (item.qualified === "Y") {
                item.qualified = {
                  code: "N",
                  message: "合格",
                };
              } else {
                item.qualified = {
                  code: "",
                  message: "",
                };
              }
              return {
                ...item,
                edit: false,
              };
            });
            this.pageOption.total = total;
          });
      }
    },
  },
  mounted() {
    this.requestListData();
  },
};
</script>

<style lang="scss" scoped></style>
